package com.bdmc.smokesensor.dao;
 
import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.bdmc.smokesensor.entity.Sys_User;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
 
public interface Sys_UserJPA extends JpaRepository<Sys_User,Integer>,JpaSpecificationExecutor<Sys_User>,Serializable {
   public Sys_User findByLoginNameAndPassword(String loginName,String password);
   // public List<Sys_User> findAll(Pageable Pageable);

   @Query(value="select new map(u,d.depName,r.roleName) from Sys_User u left join Rbac_Role r on r.id = u.rbac_RoleId"
   +" left join Sys_Department d on u.departmentId=d.id" 
   + " where d.id=:dep_id or d.treeRoot like %:treeRoot%")
   public Page<Map<String,Object>> findAllByTreeRoot(int dep_id,String treeRoot, Pageable Pageable);

   @Query("select u.loginName from Sys_User u where u.departmentId=:d_id or u.departmentId in :ids")
   public List<String> findByDepartmentIdIn(Integer d_id,List<Integer> ids);

   @Query("select u from Sys_User u left join Rbac_Role r on r.id = u.rbac_RoleId where r.roleNum=5 and (u.departmentId=:d_id or u.departmentId in :ids)")
   public List<Sys_User> findPushByDepartmentIdIn(Integer d_id,List<Integer> ids);
   
   public Sys_User findByOpenid(String openid);

   @Query("select u from Sys_User u left join Rbac_Role r on r.id=u.rbac_RoleId where u.openid=:openid and r.roleNum=:roleNum")
   public Sys_User findByOpenidAndRoleNum(String openid,String roleNum);
}
